// 懒加载
import { ref } from 'vue'
import { useIntersectionObserver, useIntervalFn } from '@vueuse/core'

// 数据懒加载功能

const useLazyData = (fn) => {
  const target = ref(null)

  const { stop } = useIntersectionObserver(
    target,
    ([{ isIntersecting }], observerElement) => {
      if (isIntersecting) {
        stop()
        fn()
      }
    },
    {
      threshold: 0.1
    }
  )
  return target
}
export const useCountdown = () => {
  const time = ref(0)
  const { pause, resume } = useIntervalFn(() => {
    time.value--
    if (time.value <= 0) {
      pause()
    }
  }, 1000, { immediate: false })

  const start = (num) => {
    time.value = num
    resume()
  }
  return { start, time }
}

export default useLazyData
